package cau.se.project.unittest;

import static org.junit.Assert.*;

import org.junit.Test;

import cau.se.project.data.CLSXTreeModel;
import cau.se.project.data.method.GroupControl;
import cau.se.project.data.method.RowControl;
import cau.se.project.method.CLSXControl;

public class MakeAndDeleteTest {

	@Test
	public void test() {
		// create tree
		CLSXControl clsx = new CLSXTreeModel();
		assertEquals(clsx.getDSM().getSize(), 0);
		
		// make row
		RowControl rc = clsx.makeRow("new row");
		assertNotNull(rc);
		assertEquals("new row", rc.getName());
		assertEquals(true, rc.isLeaf());
		assertEquals(false, clsx.isLeaf(rc));
		
		// append a row to tree
		clsx.addRow(rc);
		assertEquals(true, clsx.isLeaf(rc));
		assertEquals(rc, clsx.getChild(clsx.getRoot(), 0));
		assertNotSame(rc, clsx.getDSM().getRow(1));

		// append many rows to tree
		clsx = new CLSXTreeModel();
		for(int i=0; i<10; i++) {
			rc = clsx.makeRow("row #" + i);
			clsx.addRow(rc);
		}
		assertEquals(10, clsx.getDSM().getSize());
		
		// make group and insert childrens
		GroupControl gc = clsx.makeGroup("new group");
		assertEquals("new group", gc.getName());
		clsx.addRow(gc);
		for(int i=0; i<10; i++) {
			GroupControl root = (GroupControl) clsx.getRoot();
			rc = (RowControl) root.getChildAt(0);
			try {
				root.removeGroupMember(rc);
				gc.addGroupMember(rc);
			} catch (Exception e) {
				fail("cannot be failed");
			}
		}
		assertEquals(10, gc.getChildCount());
		
		// findrowbyname
		for(int i=0; i<10; i++)
			assertNotNull(gc.findRowByName("row #" + i));
		assertNull(gc.findRowByName("row #00"));
		assertNull(gc.findRowByName("row #10"));
		
		// set dependency
		((RowControl)gc.getChildAt(0)).setDependency((RowControl) gc.getChildAt(1));
		assertEquals(1, ((RowControl)gc.getChildAt(0)).getDepenencyList().size());
		assertEquals(gc.getChildAt(1), gc.getDepenencyList().get(0));

		((RowControl)gc.getChildAt(2)).setDependency((RowControl) gc.getChildAt(1));
		assertEquals(1, ((RowControl)gc.getChildAt(2)).getDepenencyList().size());
		assertEquals(2, gc.getDepenencyList().size());
		
		// remove dependency
		((RowControl)gc.getChildAt(2)).removeDependency((RowControl) gc.getChildAt(0));
		assertEquals(1, ((RowControl)gc.getChildAt(2)).getDepenencyList().size());
		assertEquals(2, gc.getDepenencyList().size());

		((RowControl)gc.getChildAt(2)).removeDependency((RowControl) gc.getChildAt(1));
		assertEquals(0, ((RowControl)gc.getChildAt(2)).getDepenencyList().size());
		assertEquals(1, gc.getDepenencyList().size());
		
	}

}
